package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.ProgramaTDao;
import com.sykj.why.mongo.document.entity.MenuT;
import com.sykj.why.mongo.document.entity.ProgramaT;
import com.sykj.why.mongo.dto.PageDTO;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.MenuTService;
import com.sykj.why.mongo.service.ProgramaTService;
import com.sykj.why.mongo.vo.entity.ProgramaTVo;
import com.sykj.why.util.BaseTools;
import com.sykj.why.util.DateTool;
import com.sykj.why.util.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eleven
 * @date 2019/6/4
 * @desc 栏目业务实现层
 */

@Service("ProgramaTService")
public class ProgramaTServiceImpl extends BasicServiceImpl<ProgramaT> implements ProgramaTService {

    //调用栏目数据访问层
    @Autowired
    private ProgramaTDao programaTDao;

    @Autowired
    private MenuTService menuTService;

    /**
     * 添加栏目
     * @param programaT
     */
    @Override
    public void savePrograma(ProgramaT programaT) {
        //验证栏目名称是否重复
        verifyName(programaT);
        //排序
        long count = programaTDao.count(ProgramaT.class, false);
        programaT.setSort((int) count);
        //设置时间
        programaT.setCreateTime(DateTool.getSystemTime());
        programaT.setUpdateTime(DateTool.getSystemTime());
        //保存
        programaTDao.save(programaT);
    }

    /**
     * 删除栏目
     * @param id
     */
    @Override
    public void removePrograma(String id) {
        //通过Id查询栏目
        ProgramaT programaT = findById(id);
        //逻辑删除栏目
        programaTDao.remove(programaT);
        //删除栏目下的菜单
        List<MenuT> menuTList = menuTService.findMenuListByProgramaId(id);
        if (menuTList.size()>0){
            for (MenuT menuT : menuTList){
                menuTService.removeMenu(menuT.getId());
            }
        }
    }

    /**
     * 修改栏目
     * @param programaT
     */
    @Override
    public void updatePrograma(ProgramaT programaT) {
        //通过id查询栏目
        ProgramaT programa = findById(programaT.getId());
        //重新赋值
        //验证栏目名称是否重复
        verifyName(programaT);
        programa.setProgramaName(programaT.getProgramaName());
        programa.setProramaUrl(programaT.getProramaUrl());
        programa.setUpdateTime(DateTool.getSystemTime());
        //保存或修改
        programaTDao.saveOrupdate(programa);
    }

    /**
     * 启用/禁用栏目
     * @param id
     * @param flag
     */
    @Override
    public void disableOrEnable(String id, boolean flag) {
        //通过id查询栏目
        ProgramaT programaT = findById(id);
        //修改状态
        programaTDao.disableOrEnable(programaT,flag);
    }

    /**
     * 验证栏目名称是否重复
     * @param programaT
     */
    @Override
    public void verifyName(ProgramaT programaT) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").ne(programaT.getId()).
                and("programaName").is(programaT.getProgramaName()).and("removed").is(false));
        ProgramaT programa = programaTDao.findOne(query,ProgramaT.class);
        if (programa != null) {
            throw new WhyException(ResultEnum.REPEAT);
        }
    }

    /**
     * 通过id查询栏目
     * @param id
     * @return
     */
    @Override
    public ProgramaT findById(String id) {
        //通过id查询未删除的栏目
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        ProgramaT programaT = programaTDao.findOne(query,ProgramaT.class);
        if (programaT!=null){
            return programaT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

    /**
     * 查询所有栏目
     * @return
     */
    @Override
    public List<ProgramaT> findAll() {
        //查询未删除和未禁用的所有栏目
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("enable").is(true));
        List<ProgramaT> list = programaTDao.findAll(query,ProgramaT.class);
        return list;
    }

    /**
     * 分页查询栏目
     * @param pageDTO
     * @return
     */
    @Override
    public Page findByPage(PageDTO pageDTO) {
        //查询未删除的栏目，并分页传给视图
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false));
        long count = programaTDao.count(query, ProgramaT.class);
        List<ProgramaTVo> list = new ArrayList<>();
        for (ProgramaT programaT : programaTDao.findByPage(query, pageDTO, ProgramaT.class)) {
            ProgramaTVo vo = new ProgramaTVo();
            BeanUtils.copyProperties(programaT, vo);
            vo.setCreateTime(BaseTools.longToString(programaT.getCreateTime()));
            vo.setUpdateTime(BaseTools.longToString(programaT.getUpdateTime()));
            list.add(vo);
        }
        return new Page(pageDTO, count, list);
    }

    /**
     * 根据站点地址查询站点ID
     * @param url
     * @return
     */
    @Override
    public String findByCode(String url) {
        Query query = new Query();
        query.addCriteria(Criteria.where("proramaUrl").regex(".*?"+url+".*").and("removed").is(false));
        ProgramaT programaT = programaTDao.findOne(query,ProgramaT.class);
        return programaT.getId();
    }

}
